import javax.bluetooth.RemoteDevice;
import javax.bluetooth.DataElement;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.ServiceRecord;

public class ServiceDiscoverer
        implements DiscoveryListener
{
    private static final String SERVICE_NAME = "L2CAPChat";
    private MainApp mainApp = null;
    private ServiceRecord service = null;

    public ServiceDiscoverer(MainApp mainApp){
        super();
        this.mainApp = mainApp;
    }

    public void deviceDiscovered(RemoteDevice remote,DeviceClass dClass){}

    public void inquiryCompleted(int descType){}

    public void servicesDiscovered(int transId,ServiceRecord[] services){
        for(int j=0;j<services.length;j++){
            DataElement dataElementName = services[j].getAttributeValue(0x0100);
            String serviceName = (String)dataElementName.getValue();
            if(serviceName.equals(SERVICE_NAME))
                service = services[j];
            break;
        }
    }

    public void serviceSearchCompleted(int transId,int respCode){
        String message = "";
        switch(respCode){
            case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
                message = "UILCHILGEE_HAILT_DUUSLAA";
                break;
            case DiscoveryListener.SERVICE_SEARCH_ERROR:
                message = "UILCHILGEE_HAILT_ALDAA";
                break;
            case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
                message = "UILCHILGEE_HAILT_TASLAGDLAA";
                break;
            case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
                message = "UILCHILGEE_HAILT_BICHLEG_BAIHGUI:)";
                break;
            case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
                message = "UILCHILGEE_HAILT_HANDAH_BOLOMJGUI";
                break;
        }
        //MainApp классаас дуудагдана
        mainApp.serviceSearchFinished(service,message);
        mainApp = null;
        service = null;
    }
}